package cn.luoyifan.hrm.service.impl;

import cn.luoyifan.hrm.domain.Login;
import cn.luoyifan.hrm.mapper.LoginMapper;
import cn.luoyifan.hrm.service.ILoginService;
import cn.luoyifan.hrm.service.IMealService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author whale.luoyifan
 * @since 2021-11-22
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {

    @Autowired
    private IMealService mealService;

    //后台用的方法
    @Override
    public Login saveLogininAndMeal(Login login, Long meadId) {
        //校验
        if (StringUtils.isEmpty(login.getUsername())){
            throw new RuntimeException("用户名不能为空");
        }
        if (StringUtils.isEmpty(login.getPassword())){
            throw new RuntimeException("密码不能为空");
        }
        if (meadId == null){
            throw new RuntimeException("套餐不能为空");
        }
        //保存login，只允许保存：username password type
        //密码加密
        //账户启用
        login.setEnabled(true);
        login.setAccountNonExpired(true);
        login.setAccountNonLocked(true);
        login.setCredentialsNonExpired(true);
        insert(login);
        //保存套餐
        Long loginId = login.getId();

        mealService.saveRelationWithLogin(meadId,loginId);

        return login;
    }
}
